﻿using Engine;
using Engine.Graphics;
using Game;

namespace SAGhoul.Blocks
{
    public class SplittingSpearBlock : Block  //分裂射击
    {
        public BlockMesh m_handleMesh = new();
        public BlockMesh m_headMesh = new();
        private Texture2D m_texture;

        public override void Initialize()
        {
            // 加载资源
            m_texture = ContentManager.Get<Texture2D>("Textures/spear/SplittingSpear");
            Model model = ContentManager.Get<Model>("Models/DynamicSpear");

            // 手柄部分
            Matrix handleTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Handle").ParentBone)
                * Matrix.CreateTranslation(0f, -0.5f, 0f);
            m_handleMesh.AppendModelMeshPart(
                model.FindMesh("Handle").MeshParts[0],
                handleTransform,
                false, false, false, false, Color.White
            );

            // 头部部分
            Matrix headTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Head").ParentBone)
                * handleTransform;
            m_headMesh.AppendModelMeshPart(
                model.FindMesh("Head").MeshParts[0],
                headTransform,
                false, false, false, false, Color.LightYellow
            );

            base.Initialize();
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color,
            float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            // 渲染手柄（静态部分）
            BlocksManager.DrawMeshBlock(
                primitivesRenderer,
                m_handleMesh,
                m_texture,
                color,
                2f * size,
                ref matrix,
                environmentData
            );

            // 计算旋转角度（基于游戏时间）
            float rotation = (float)(Time.FrameStartTime * 2.5f) % (2f * MathUtils.PI);

            // 构建头部变换矩阵（只旋转不位移）
            Matrix headMatrix = Matrix.CreateRotationY(rotation) * matrix;

            // 渲染头部（动态旋转部分）
            BlocksManager.DrawMeshBlock(
                primitivesRenderer,
                m_headMesh,
                m_texture,
                color,
                2f * size,
                ref headMatrix,
                environmentData
            );
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator,
            TerrainGeometry geometry, int value, int x, int y, int z)
        {
            // 空实现，不生成地形碰撞
        }
    }
}